myApp.controller('EmergencyListController', function ($scope, $stateParams, ngDialog, $http, $uibModal, $interval) {
    $scope.info = {};
    $scope.info.planList = [];
    $scope.info.isShowOnline = false;
    $scope.info.modifiAble = true;
    $scope.timer = null;
    $scope.info.qaList = [];

    if (!isUserCanOperateByRole('plan_admin', 'pm')) {
        $scope.info.modifiAble = false;
    }

    $scope.$on("$destroy", function () {
        if ($scope.timer != null) {
            console.log("清除项目定时器;");
            $interval.cancel($scope.timer);
        }
    });

    doBeetleGet($http, $uibModal, python_url + '/api/v1/user/?role_name=qa&token_id=' + getTokenId(), function (data) {
        $scope.info.qaList = data.data;
    });

    $scope.planList = function () {
        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/project_plan/getPlanList?token_id=' + getTokenId() + '&isEmergency=0&isShowOnline=' + $scope.info.isShowOnline).success(function (data) {
            $scope.info.planList = data.data;
            $scope.startTimer();
        }).finally(function () {
            closeModal();
        });
    };

    $scope.goHelp= function(){
        window.open("http://guide2.winit.com.cn/pages/viewpage.action?pageId=20840927");
    };

    $scope.planList();

    $scope.total = $scope.info.planList.length;
    $scope.page = {size: 20, index: 1};

    $scope.startTimer = function () {
        if ($scope.timer != null) {
            //校验是否还需要定时，不需要则取消
            $scope.info.isNeedTimer = false;
            for (var i = 0; i < $scope.info.planList.length; i++) {
                if ($scope.info.planList[i].notCreateProCount != 0) {
                    $scope.info.isNeedTimer = true;
                }
            }
            if (!$scope.info.isNeedTimer) {
                $interval.cancel($scope.timer);
            }
            return;
        }
        angular.forEach($scope.info.planList, function (plan) {
            if (plan.notCreateProCount != 0) {
                if ($scope.timer == null) {
                    $scope.timer = $interval(function () {
                        //清空缓存
                        $http.post(beetle_plan_url + '/cache/clearCache').success(function (data) {
                        });
                        $scope.planList()
                    }, 20000);
                }
            }
        })
    };

    $scope.projectInfo = function (id) {
        window.location.href = "#/menu/emergencyInfo/" + id;
    };

    $scope.add = function () {
        window.location.href = "#/menu/plan_Guide/1";
    };

    $scope.del = function (plan_id) {
        if (!$scope.info.modifiAble) {
            return;
        }
        openDialog(ngDialog, $scope, "删除不可恢复，请确认！", true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, "是否要删除项目内工程的svn目录?", true, function (val) {
                doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/delPlan?token_id=' + getTokenId() + '&plan_id=' + plan_id + '&type=2' + '&is_del_svn=' + val, function (data) {
                    if (data.success) {
                        $scope.planList();
                    } else {
                        openDialogAlert(ngDialog, $scope, data.msg);
                    }
                })
            });
        })
    };
    $scope.updateTestUser = function (plan) {
        plan.modifyTestUser = !plan.modifyTestUser;
        if (!plan.modifyTestUser) {
            var data = {
                token_id: getTokenId(),
                plan_id: plan.plan_id,
                test_head: plan.testUserInfo.account,
                test_head_name: plan.testUserInfo.realname
            };
            doBeetlePost($http, $uibModal, beetle_plan_url + '/project_plan/updatePlanTestHead', data, function (data) {
                if (data.success) {
                    $scope.planList();
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        } else {
            angular.forEach($scope.info.qaList, function (qa) {
                if (qa.account == plan.test_head) {
                    plan.testUserInfo = qa;
                    return
                }
            })
        }
    }
});